package com.ali.resource.ServicePage;

import com.ali.resource.CompoentPage.SslComponent;
import com.ali.resource.MapPage.dns.DnsPrivateZoneRecordMap;
import com.ali.resource.MapPage.ssl.SslMap;
import com.ali.resource.ShareTools.BasicTools;
import com.aliyun.cas20200407.Client;
import com.aliyun.cas20200407.models.*;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestParam;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Component
public class SslService {

    @Autowired
    private SslComponent sslComponent;


    public SslMap getSslMap(ListUserCertificateOrderResponseBody.ListUserCertificateOrderResponseBodyCertificateOrderList certInfo){
        SslMap build = SslMap.builder()
                .name(certInfo.getName())
                .masterDns(certInfo.getCommonName())
                .sans(certInfo.getSans())
                .status(certInfo.getStatus())
                .upload(certInfo.getUpload().toString())
                .startDate(certInfo.getStartDate())
                .endDate(certInfo.getEndDate()).build();
        return build;
    }


    public List<SslMap> getCertList(
            @RequestParam(required = false) String sanPrefix) throws Exception {


        Long totalCount = sslComponent.getListResponse(sslComponent.getListRequest()).getBody().getTotalCount();

        long l = totalCount / 50;
        if (0 != (totalCount % 50)) {
            l += 1L;
        }

        ArrayList<SslMap> sslMaps = new ArrayList<>();

        for (long i = 1; i < l + 1; i++) {
            ListUserCertificateOrderRequest listRequest = sslComponent.getListRequest();
            listRequest.setCurrentPage(i);
            listRequest.setShowSize(50L);
            List<ListUserCertificateOrderResponseBody.ListUserCertificateOrderResponseBodyCertificateOrderList> certificateOrderList = sslComponent.getListResponse(listRequest).getBody().getCertificateOrderList();

            for (ListUserCertificateOrderResponseBody.ListUserCertificateOrderResponseBodyCertificateOrderList certInfo : certificateOrderList) {
                if (sanPrefix != null && certInfo.getSans().toLowerCase().contains(sanPrefix)){
                    SslMap sslMap = this.getSslMap(certInfo);
                    sslMaps.add(sslMap);
                } else  if (sanPrefix == null){
                    SslMap sslMap = this.getSslMap(certInfo);
                    sslMaps.add(sslMap);
                }

            }

        }

        return sslMaps;

    }


    public void getSslExecl(List<SslMap> sslMaps) throws IOException {
        XSSFWorkbook sheets = new XSSFWorkbook();
        XSSFSheet sheet = sheets.createSheet();
        XSSFRow row = sheet.createRow(0);

        row.createCell(0).setCellValue("name");
        row.createCell(1).setCellValue("sans");
        row.createCell(2).setCellValue("status");
        row.createCell(3).setCellValue("startDate");
        row.createCell(4).setCellValue("endDate");
        row.createCell(5).setCellValue("upload");
        row.createCell(6).setCellValue("masterDns");

        int number = 1;

        for (SslMap sslMap : sslMaps) {
            XSSFRow row1 = sheet.createRow(number);
            row1.createCell(0).setCellValue(sslMap.getName());
            row1.createCell(1).setCellValue(sslMap.getSans());
            row1.createCell(2).setCellValue(sslMap.getStatus());
            row1.createCell(3).setCellValue(sslMap.getStartDate());
            row1.createCell(4).setCellValue(sslMap.getEndDate());
            row1.createCell(5).setCellValue(sslMap.getUpload());
            row1.createCell(6).setCellValue(sslMap.getMasterDns());


            number += 1;
        }

        File file = BasicTools.setFIlePath("ssl.xlsx");

        FileOutputStream fileOutputStream = new FileOutputStream(file);
        sheets.write(fileOutputStream);
        fileOutputStream.close();
        sheets.close();
    }

}
